﻿using Ec_System.API;
using Ec_System.Models.Web;
using Ec_System.Service.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ec_System.Bll.Web
{
    public class QrCodeManager
    {
        /// <summary>
        /// 根据溯源码获取Product的溯源信息
        /// </summary>
        /// <param name="code">溯源码</param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static QrCodeData GetQrInfoByCode(string code, string ip)
        {
            QrCodeData model = new QrCodeData();
            //5分钟之内不允许第二次提交
            MemoryCacheManager mem = new MemoryCacheManager();
            string result = mem.Get<string>(ip);
            if (result == null)
            {
                mem.Set(ip.Replace(".", "").Replace(";", ""), ip, 1);
                //返回结果
                QrCodeService service = new QrCodeService();
                model = service.GetQrInfoByCode(code, ip);
                if (model == null)
                {
                    model = new QrCodeData();
                    model.IsNull = true;
                    model.ErrorMessage = "没有溯源信息！";
                }
                else
                {
                    model.IsNull = false;
                    
                }
            }
            else
            {
                model = new QrCodeData();
                model.IsNull = true;
                model.ErrorMessage = "不允许频繁提交！";
            }
            return model;
        }
    }
}
